package org.lwd.datalink.domain.media;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.type.Alias;
import org.lwd.datalink.domain.BaseEntity;
import org.lwd.datalink.domain.Storable;
import org.lwd.datalink.domain.media.parameter.MediaSrcParameter;
import org.lwd.datalink.errors.DatalinkException;

import java.io.Serializable;

/**
 * @Author: weidong.lin
 * @Description: 介质(数据)源数据类
 * @Date: create in 2024/4/2 14:20
 */
@Setter
@Getter
@Alias("mediaSource")
public class MediaSourceInfo extends BaseEntity implements Serializable, Storable {
    private Long id;
    private String name;
    private MediaSourceType type;
    private String desc;
    private String parameter;
    private MediaSourceType simulateMsType;

    public void setParameter(String parameter) {
        this.parameter = parameter;
        if (!StringUtils.isEmpty(parameter)) {
            parameterObj = JSONObject.parseObject(parameter, new TypeReference<MediaSrcParameter>() {}, Feature.SupportAutoType);
        } else {
            parameterObj = null;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        MediaSourceInfo that = (MediaSourceInfo) o;

        return id.equals(that.id);

    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    //------------------------------------------------------------------------------------------------------------------
    //-----------------------------------------fields for business------------------------------------------------------
    //------------------------------------------------------------------------------------------------------------------

    private transient MediaSrcParameter parameterObj;


    @SuppressWarnings("unchecked")
    public <T extends MediaSrcParameter> T getParameterObj() {
        if (parameterObj == null && parameter != null) {
            throw new DatalinkException("MediaSrcParameter can not be null.");
        }
        return (T) parameterObj;
    }
}
